<template>
	<div class="">
		<div class="crumbs">
			<h2 class="">机构认领</h2>
		</div>
		<div class="container ">
			<div class="handle-box margin-bottom20 row-me row-center">
				<el-form :model="query" label-width="80px" class="ipt row-me">
					<el-form-item label="机构名称">
						<el-input v-model="query.organName" placeholder="请输入"></el-input>
					</el-form-item>
					<el-form-item label="地区">
						<el-cascader v-model="query.area" :options="areaList"
							:props="{ value: 'adcode', label: 'name', expandTrigger: 'hover' }" @change="handleChange"></el-cascader>
					</el-form-item>
					<el-form-item label="认领用户">
						<el-input v-model="query.key" placeholder="请输入"></el-input>
					</el-form-item>
				</el-form>

				<div class="margin-bottom20 margin-left20">
					<el-button type="primary" @click="handleSearch">查询</el-button>
					<el-button @click="resetClick">重置</el-button>
					<el-button type="primary" class="handle-del" @click="exportClick">批量导出
					</el-button>
				</div>
			</div>
			<el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"
				@selection-change="handleSelectionChange">
				<el-table-column type="selection" width="40" align="center"></el-table-column>
				<el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
				<el-table-column prop="organName" label="机构简称" align="center"></el-table-column>
				<el-table-column prop="companyName" label="公司名称" align="center"></el-table-column>
				<el-table-column prop="name" label="地区" align="center" show-overflow-tooltip>
					<template slot-scope="scope">
						{{scope.row.aaa}}
						<span v-if="!$.isEmpty(scope.row.bbb)">,</span>
						{{scope.row.bbb}}
						<span v-if="!$.isEmpty(scope.row.ccc)">,</span>
						{{scope.row.ccc}}
					</template>
				</el-table-column>
				<el-table-column prop="userName" label="认领用户" align="center"></el-table-column>
				<el-table-column prop="userPhone" label="用户电话" align="center"></el-table-column>
				<el-table-column prop="claimTime" label="认领时间" align="center"></el-table-column>

			</el-table>
			<div class="pagination">
				<el-pagination background layout="total, prev, pager, next" :current-page="query.pageNumber"
					:page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
			</div>
		</div>

	</div>
</template>

<script>
	export default {
		name: 'categoryTable',
		data() {
			return {
				query: {
					pageNumber: 1,
					pageSize: 30
				},
				dialogTitle: '',
				tableData: [],
				multipleSelection: [],
				editVisible: false,
				pageTotal: 0,
				form: {},
				areaList: [], //地区
				approveList: [],

			};
		},
		created() {
			this.getData();
			this.getProvinceWay();
		},
		methods: {
			getTreeData(data) {
				// 循环遍历json数据
				for (var i = 0; i < data.length; i++) {
					if (data[i].level == 3) { //等于3的时候把第4层children-设为-undefined
						// children若为空数组，则将children设为undefined
						data[i].children = undefined;
					} else {
						// children若不为空数组，则继续 递归调用 本方法
						this.getTreeData(data[i].children);
					}
				}
				return data;
			},
			// 获取省市区
			getProvinceWay() {
				this.$get('/admin/area/getAllArea', {}).then((res) => {
					if (res.code == 1000) {
						this.areaList = this.getTreeData(res.data); //获取树结构值
					}
				}).catch((error) => {
					console.log(error)
				})
			},
			parseParams(data) {
				try {
					var tempArr = [];
					for (var i in data) {
						var key = encodeURIComponent(i);
						var value = encodeURIComponent(data[i]);
						tempArr.push(key + '=' + value);
					}
					var urlParamsStr = tempArr.join('&');
					return urlParamsStr;
				} catch (err) {
					return '';
				}
			},
			// 批量导出
			exportClick() {
				if (this.multipleSelection.length == 0) {
					this.$message.warning('请先选择数据!')
					return
				}

				var list = [];
				this.multipleSelection.forEach((item, index) => {
					list.push(item.id)
				});

				let params = {};
				// params.pageSize = 1000000000;
				// let appid = localStorage.getItem('htk_appid');
				params.token = localStorage.getItem('token');
				params.ids = list;
				window.open(this.$host + '/admin/applys/organExport?' + this.parseParams(params));
			},
			// 选择省区
			handleChange(value) {
				console.log(value);
				this.query.citySheng = this.query.area[0];
				this.query.cityShi = this.query.area[1];
				this.query.cityXian = this.query.area[2];
			},
			// 获取 easy-mock 的模拟数据
			getData() {
				this.$postForm('/admin/applys/organAll', this.query).then((res) => {
					if (res.code == 1000) {
						this.$forceUpdate(); //强制刷新
						if (res.data.list.length > 0) {

							res.data.list.forEach((item, index) => {
								item.aaa = [];
								item.bbb = [];
								item.ccc = [];
								if (!this.$.isEmpty(item.shengNames)) {
									if (item.shengNames.length > 0) {
										item.shengNames.forEach((item1, index1) => {
											item.aaa.push(item1.name)
										});
									}
								}
								item.aaa = item.aaa.join(",");

								if (!this.$.isEmpty(item.shiNames)) {
									if (item.shiNames.length > 0) {
										item.shiNames.forEach((item1, index1) => {
											item.bbb.push(item1.name)
										});
									}
								}
								item.bbb = item.bbb.join(",");
								if (!this.$.isEmpty(item.xianNames)) {
									if (item.xianNames.length > 0) {
										item.xianNames.forEach((item1, index1) => {
											item.ccc.push(item1.name)
										});
									}
								}
								item.ccc = item.ccc.join(",");
							});

							this.tableData = res.data.list;
						} else {
							this.tableData = [];
						}

						this.pageTotal = res.data.totalRow
					}
				}).catch((error) => {
					console.log(error)
				})

			},
			// 触发搜索按钮
			handleSearch() {
				if (!this.$.isEmpty(this.query.area)) {
					if (this.query.area.length > 0) {
						this.query.citySheng = this.query.area[0];
						this.query.cityShi = this.query.area[1];
						this.query.cityXian = this.query.area[2];
					}
				}
				this.query.pageNumber = 1;
				this.getData();
			},
			// 点击重置
			resetClick() {
				this.query = {
					pageNumber: 1,
					pageSize: 30
				}
				this.query.area = [];
				this.getData();
			},
			// 多选操作
			handleSelectionChange(val) {
				this.multipleSelection = val;
			},
			// 编辑操作
			handleEdit(index, row) {
				this.title = '编辑排名';
				this.idx = index;
				this.form = row;
				this.rankingShow = true;
			},

			// 分页导航
			handlePageChange(val) {
				this.query.pageNumber = val
				this.getData();
			}
		}
	};
</script>

<style>
</style>